﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class SupplierController : BaseController
    {
        //
        // GET: /Admin/Supplier/

        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var supplierService = new SupplierService(adp);
            var page = Query.Params("pageNum");
            var jianma = Query.Post("JianMa");
            var Name = Query.Post("Name");
            ViewBag.jianma = jianma;
            ViewBag.name = Name;
         
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }

            var where = PredicateBuilder.True<Supplier>();
            where = where.And(a => a.IsDel == false);
            if(!string.IsNullOrEmpty(jianma))
            {
                where = where.And(a => a.JianMa.Contains(jianma));
            }
            if (!string.IsNullOrEmpty(Name))
            {
                where = where.And(a => a.Name.Contains(Name));
            }
            var suppliers = supplierService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(1), PageSize);
            BindPage(suppliers.ItemCount, page.ToInt(), PageSize);

            ViewBag.Records = suppliers.ItemCount;

            return View(suppliers.ItemCollection.ToList());
        }

        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt(0);
            if(id>0)
            {
                var adp = new DbContextAdapter();
                var supplierService = new SupplierService(adp);
                var model = supplierService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(model);
            }
            return View();
        }

        [HttpPost]
        public JsonResult Edit(Supplier model)
        {
            if (model == null) throw new ArgumentNullException("model");
            var adp = new DbContextAdapter();
            var supplierService = new SupplierService(adp);
            var dicService = new DicService(adp);
            model.JianMa = dicService.GetChineseFirstSpell(model.Name);
            supplierService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "supplierIndex");
        }

        public JsonResult Delete()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var supplierService = new SupplierService(adp);
            var supplierModel = supplierService.Repository.Find(a => a.ID == id).FirstOrDefault();
            if (supplierModel != null)
            {
                supplierService.Repository.Delete(supplierModel);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "supplierIndex");
        }



    }
}
